package CCRMClient.GUI;

import javax.swing.JPanel;
import javax.swing.JLabel;

import CCRMClient.ClientStart;
import CCRMClient.Controllers.ClientSaleManager;
import Common.SERVER_COMMANDS;

import java.awt.Font;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.table.DefaultTableModel;
import javax.swing.ListSelectionModel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.SwingConstants;
/**
 * This screen is for showing all the sales of the user.
 * @author Oz Mouchtar
 *
 */
public class SalesScreen extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private JLabel lblSalesFor;
	private JButton btnGoBack;
	private JButton btnViewSaleDetails;
	private JScrollPane scpMySales;
	private JTable tblMySales;
	private Integer saleId;
	private ArrayList<String[]> sales;
	
	public SalesScreen(ArrayList<String[]> sales) {
		this.sales = sales;
		setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		initComponents();
	}
	private void initComponents() {
		setSize (474, 335);
		setBackground(Color.WHITE);
		setLayout(null);
		
		lblSalesFor = new JLabel("Sales of: \"" + ClientStart.user.getName() + "\"");
		lblSalesFor.setHorizontalAlignment(SwingConstants.CENTER);
		lblSalesFor.setForeground(Color.RED);
		lblSalesFor.setBounds(10, 11, 474, 27);
		lblSalesFor.setFont(new Font("David", Font.BOLD, 26));
		add(lblSalesFor);
		
		add(getScpMySales());
		loadMySales();
		
		btnGoBack = new JButton("Go Back");
		btnGoBack.addActionListener(new BtnGoBackActionListener());
		btnGoBack.setBounds(30, 247, 107, 23);
		btnGoBack.setFont(new Font("Tahoma", Font.BOLD, 16));
		add(btnGoBack);
		
		btnViewSaleDetails = new JButton("View Sale details");
		btnViewSaleDetails.addActionListener(new BtnViewSaleDetailsActionListener());
		btnViewSaleDetails.setEnabled(true);
		btnViewSaleDetails.setBounds(230, 247, 187, 23);
		btnViewSaleDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
		add(btnViewSaleDetails);
	}
	
	private void loadMySales() {
		if (!sales.isEmpty()) {
			DefaultTableModel dtm = (DefaultTableModel) tblMySales.getModel();
			dtm.removeRow(0);
			for (String[] row : sales)
				dtm.addRow(row);
		}
	}
	
	JScrollPane getScpMySales() {
		if (scpMySales == null) {
			scpMySales = new JScrollPane();
			scpMySales.setBounds(20, 49, 412, 187);
			scpMySales.setViewportView(getTbMySales());
		}
		return scpMySales;
	}
	
	JTable getTbMySales() {
		if (tblMySales == null) {
			tblMySales = new JTable();
			tblMySales.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			tblMySales.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null},
				},
				new String[] {
					"Sale ID", "Sale Name"
				}
			) {
				Class[] columnTypes = new Class[] {
					String.class, String.class
				};
				public Class getColumnClass(int columnIndex) {
					return columnTypes[columnIndex];
				}
			});
			tblMySales.setFont(new Font("David", Font.BOLD, 11));
		}
		return tblMySales;

	}
	
	/**
	 * This inner class implements event listener for pressing View Sale Details button
	 * @author Ori Ziv
	 *
	 */
	private class BtnViewSaleDetailsActionListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			int selected = tblMySales.getSelectedRow();
			if (selected == -1) {
				JOptionPane.showMessageDialog(null, "Please select a sale", "Display Sale", JOptionPane.INFORMATION_MESSAGE);
			}
			DefaultTableModel dtm = (DefaultTableModel) tblMySales.getModel();
			String saleID = (String) dtm.getValueAt(selected, 0);
			try {
				ClientStart.client.handleMessageFromGUI(SERVER_COMMANDS.SHOW_SALE_DETAILS, ClientSaleManager.getSaleDetails(SERVER_COMMANDS.SHOW_SALE_DETAILS, Integer.parseInt(saleID)));
			} catch (IOException e) {
				JOptionPane.showMessageDialog(null, "Lost connection with server", "Communication Error", JOptionPane.ERROR_MESSAGE);
				ClientStart.quitApp();
			}
		}
	}
	private class BtnGoBackActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ClientStart.panelSwicher(new EmployeePortal());
		}
	}
}
